package com.softer.chengxin.param.controller;

import com.github.pagehelper.PageInfo;
import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.login.service.impl.LoginServiceimpl;
import com.softer.chengxin.param.entity.ParamVO;
import com.softer.chengxin.param.service.ParamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.time.LocalDate;

/**
 * @author Gong Zhiyang
 * @date 2019/12/11
 */
@RestController
@RequestMapping("/api/param")
public class ParamController {
    @Autowired
    private ParamService paramService;
    @Autowired
    private LoginServiceimpl loginService;

    /**
     * 获取当前日期
     *
     * @return 返回当前日期
     */
    @PostMapping("/getLocalDate")
    public String getLocalDate() {
        return LocalDate.now().toString();
    }

    /**
     * 修改参数
     *
     * @param paramVO 参数
     * @param session session
     * @return 修改结果
     */
    @PostMapping("/update")
    public CommonResult update(ParamVO paramVO, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return result;
        }
        return paramService.update(paramVO);
    }

    /**
     * 获取参数详情
     *
     * @param id      参数Id
     * @param session session
     * @return 参数详情
     */
    @PostMapping("/getInfo")
    public ParamVO getInfo(int id, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new ParamVO();
        }
        return paramService.getInfo(id);
    }

    /**
     * 获取参数列表
     *
     * @param page    分页实体
     * @param paramVO 查询实体
     * @param session session
     * @return 参数列表
     */
    @PostMapping("/getList")
    public PageInfo<ParamVO> getList(Page page, ParamVO paramVO, HttpSession session) {
        //  登录名
        String loginName = (String) session.getAttribute(Common.LOGIN_NAME);
        CommonResult result = loginService.checkLoginName(loginName);
        if (ResultEnum.FAIL.getCode() == result.getData()) {
            return new PageInfo<>();
        }
        return paramService.getList(page, paramVO);
    }
}
